package com.zenchn.service.impl;

import com.zenchn.dao.TSysAccountMapper;
import com.zenchn.dynamic.TSysAccountDynamicMapper;
import com.zenchn.enums.CommonStatusEnum;
import com.zenchn.generic.GenericDao;
import com.zenchn.generic.GenericServiceImpl;
import com.zenchn.model.TSysAccount;
import com.zenchn.model.TSysAccountExample;
import com.zenchn.service.AccountService;
import javax.annotation.Resource;

import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static org.mybatis.dynamic.sql.SqlBuilder.*;
import static com.zenchn.dynamic.TSysAccountDynamicSqlSupport.*;
import java.util.List;

/**
 * 用户Service实现类
 *
 * @author dingxinfa
 * @date 2020-04-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AccountServiceImpl extends GenericServiceImpl<TSysAccount, String, TSysAccountExample> implements AccountService {

    @Resource
    private TSysAccountMapper accountMapper;

    @Resource
    private TSysAccountDynamicMapper accountDynamicMapper;

    @Override
    public GenericDao<TSysAccount, String, TSysAccountExample> getDao() {
        return accountMapper;
    }



    @Override
    public TSysAccount authentication(String account, String password) {
        TSysAccountExample example = new TSysAccountExample();
        example.createCriteria().andAccountEqualTo(account).andPasswordEqualTo(password);
        example.setOrderByClause("account limit 1");
        final List<TSysAccount> list = accountMapper.selectByExample(example);
        return null!=list && !list.isEmpty()?list.get(0):null;
    }


    @Override
    public TSysAccount selectByAccount(String account) {
    	TSysAccountExample example = new TSysAccountExample();
        example.createCriteria().andAccountEqualTo(account);
        example.setOrderByClause("account limit 1");
        final List<TSysAccount> list = accountMapper.selectByExample(example);
        return null!=list && !list.isEmpty()?list.get(0):null;
    }

    @Override
    public Boolean verifyAccount(String accountId, String account) {
        TSysAccountExample example = new TSysAccountExample();
        TSysAccountExample.Criteria criteria = example.createCriteria();

        criteria.andAccountEqualTo(account);
        if (null != accountId && !"".equals(accountId.trim())) {
            criteria.andAccountIdNotEqualTo(accountId.trim());
        }

        return accountMapper.countByExample(example) <= 0;
    }



    @Override
    public List<TSysAccount> selectDynamicSqlByParameter(String notAccountId,String keyword,Integer status) {


        QueryExpressionDSL<SelectModel> dsl=select(TSysAccount.allColumns()).from(TSysAccount);
        QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder dslWhere= dsl.where(TSysAccount.status,null!=status?isEqualTo(status):isGreaterThanOrEqualTo(CommonStatusEnum.DISABLE.getCode()));
        if (null!=notAccountId && !"".equals(notAccountId.trim())){
            dslWhere.and(TSysAccount.accountId,isNotEqualTo(notAccountId.trim()));
        }
        if (null!=keyword  && !"".equals(keyword.trim())){
            keyword="%"+keyword.trim()+"%";
            dslWhere.and(TSysAccount.account,isLike(keyword),or(TSysAccount.realName,isLike(keyword)),or(TSysAccount.tel,isLike(keyword)));
        }
        SelectStatementProvider selectStatement=dsl.orderBy(TSysAccount.createTime.descending())
                .build()
                .render(RenderingStrategies.MYBATIS3);
        return accountDynamicMapper.selectMany(selectStatement);

    }


}
